package com.xiaoyun.utils;

import com.xiaoyun.vo.Feedback;
import com.xiaoyun.vo.GainPassTemplateRequest;
import com.xiaoyun.vo.PassTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * Rowkey 生成器工具类
 */
@Slf4j
public class RowKeyGenUtil {
    /**
     * @param passTemplate
     * @return
     */
    public static String genPassTemplateRowKey(PassTemplate passTemplate) {
        String pasInfo = String.valueOf(passTemplate.getId()) + "_" + passTemplate.getTitle();
        String rowKey = DigestUtils.md5Hex(pasInfo);
        log.info("GenPassTemplateRowKey:{} , {}", pasInfo, rowKey);
        return rowKey;
    }

    /**
     * 根据提供的领取优惠券请求生成RowKey 之可以在领取优惠券的时候使用
     * Pass RowKey = reversed(userId)  + inverse(timestmap) + PassTemplate RowKey
     *
     * @param request
     * @return
     */
    public static String genPassRowKey(GainPassTemplateRequest request) {
        return new StringBuilder(String.valueOf(request.getUserId())).reverse().toString()
                + (Long.MAX_VALUE - System.currentTimeMillis())
                + genPassTemplateRowKey(request.getPassTemplate());
    }

    /**
     * 根据Feedback 构造RowKey
     *
     * @param feedback
     * @return
     */
    public static String genFeedbackRowKey(Feedback feedback) {
        return new StringBuilder(String.valueOf(feedback.getUserId())).reverse().toString() + (Long.MAX_VALUE - System.currentTimeMillis());

    }
}
